package com.hzy.fivepalace.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzy.fivepalace.admin.annotation.Login;
import com.hzy.fivepalace.admin.controller.base.BaseController;
import com.hzy.fivepalace.common.core.AjaxResult;
import com.hzy.fivepalace.common.page.PageUtils;
import com.hzy.fivepalace.common.page.TableDataInfo;
import com.hzy.fivepalace.common.util.DateUtils;
import com.hzy.fivepalace.db.domain.DtsGoods;
import com.hzy.fivepalace.db.domain.DtsGroupProduct;
import com.hzy.fivepalace.db.service.DtsGoodsService;
import com.hzy.fivepalace.db.service.DtsGroupOpenDetailService;
import com.hzy.fivepalace.db.service.DtsGroupOpenListService;
import com.hzy.fivepalace.db.service.DtsGroupProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 商品拼团服务
 *
 * @author gejian
 */
@RestController
@RequestMapping("/admin/group")
@Api(tags = "商品拼团")
@ResponseBody
@Login
public class AdminGoodsGroupController extends BaseController {

    @Resource
    private DtsGroupOpenListService groupOpenListService;
    @Resource
    private DtsGroupOpenDetailService groupOpenDetailService;
    @Resource
    private DtsGroupProductService groupProductService;
    @Resource
    private DtsGoodsService goodsService;


    @Value("${sys-config.imgUrl}")
    private String imgUrl;


    /**
     * 查询拼团商品列列表
     */
    @ApiOperation("查询拼团商品列表")
    @PostMapping("/goods/list")
    public TableDataInfo<DtsGroupProduct> list(@Validated DtsGroupProduct mallGroupProduct) {
        QueryWrapper<DtsGroupProduct> qw = new QueryWrapper<>();
        if(mallGroupProduct.getLinkId() !=null){
            qw.eq("link_id",mallGroupProduct.getLinkId());
        }
        if(mallGroupProduct.getState() !=null){
            qw.eq("state",mallGroupProduct.getState());
        }
        if(StrUtil.isNotEmpty(mallGroupProduct.getTitle())){
            qw.like("title", mallGroupProduct.getTitle());
        }
        qw.orderByDesc("id");


        TableDataInfo<DtsGroupProduct> td = PageUtils.buildDataInfo(groupProductService.page(PageUtils.buildPage(),qw));
        List<DtsGroupProduct> list = td.getRows();
        if(CollUtil.isNotEmpty(list)){
            for (DtsGroupProduct groupProduct : list){
                DtsGoods goods = goodsService.findById(groupProduct.getLinkId());
                groupProduct.setGoods(goods);

                if(StrUtil.isNotEmpty(groupProduct.getActiveImg())){
                    groupProduct.setActiveImgPath(imgUrl + groupProduct.getActiveImg());
                }
            }
        }
        return td;
    }

    /**
     * 删除商品保障
     *
     * @param groupId
     * @return
     */
    @PostMapping("/detail")
    @ApiOperation(value = "详情")
    public AjaxResult<DtsGroupProduct> getInfo(@ApiParam(value = "拼团ID") @RequestParam @NotNull Integer groupId) {
        DtsGroupProduct mallGroupProduct = groupProductService.getById(groupId);
        if(mallGroupProduct!=null){
            if(StrUtil.isNotEmpty(mallGroupProduct.getActiveImg())){
                mallGroupProduct.setActiveImgPath( imgUrl + mallGroupProduct.getActiveImg());
            }

            if(mallGroupProduct.getLinkId() != null && mallGroupProduct.getLinkId() > 0){
                DtsGoods product = goodsService.findById(mallGroupProduct.getLinkId());
                mallGroupProduct.setGoods(product);
            }

            if(mallGroupProduct.getBeginTime()!=null && mallGroupProduct.getEndTime() !=null) {
                String[] dateRange = {DateUtils.dateTime(mallGroupProduct.getBeginTime()),DateUtils.dateTime(mallGroupProduct.getEndTime())} ;
                mallGroupProduct.setDateRange(dateRange);
            }
        }
        return AjaxResult.success(mallGroupProduct);
    }

    /**
     * 删除商品保障
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public AjaxResult delGroup(@ApiParam(value = "主键") @RequestParam @NotNull Integer id) {
        return AjaxResult.success(groupProductService.removeById(id));
    }

    /**
     * 新增商品团购
     *
     * @param groupProduct
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增")
    public AjaxResult addGoodsGroup(DtsGroupProduct groupProduct) {
        groupProduct.setCreateTime(new Date());
        return AjaxResult.success(groupProductService.save(groupProduct));
    }

    /**
     * 修改商品团购
     *
     * @param groupProduct
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public AjaxResult updGoodsSecurity(DtsGroupProduct groupProduct) {
        String[] dateRange = groupProduct.getDateRange();
        if(dateRange!= null && dateRange.length == 2){
            String beginTime = dateRange[0] + " 00:00:00";
            String endTime = dateRange[1] + " 23:59:59";

            groupProduct.setBeginTime(DateUtils.parseDate(beginTime));
            groupProduct.setEndTime(DateUtils.parseDate(endTime));
        }
        return AjaxResult.success(groupProductService.updateById(groupProduct));
    }

}
